/**
 * Created by dukes on 2015/10/20.
 */
class TravelPanel extends eui.Component{
    constructor(){
        super();
        this.name = "travelpanel";
        this.skinName = "skins.scene.TravelSceneSkin";
        this.missionProvider = new eui.ArrayCollection();
        // this.dailyMissionItemRender = DailyMissionItemRender;
        this.addEventListener(egret.Event.ADDED_TO_STAGE,this.addedToStage, this );
        this.addEventListener(egret.Event.REMOVED_FROM_STAGE, this.removedFromStage, this);
    }

    private addedToStage(evt:egret.Event){
        this.img_page_right.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onPage,this);
        this.img_page_left.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onPage, this);
        this.img_adopt.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onAdopt, this);
        this.img_upgrade.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onUpgrade, this);
        this.img_chosen.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onRear, this);
        this.img_not_chosen.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onRear, this);
        this.updatePageDisplay();
    }
    private removedFromStage(evt:egret.Event){
        this.img_page_right.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onPage,this);
        this.img_page_left.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onPage, this);
        this.img_adopt.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onAdopt, this);
        this.img_upgrade.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onUpgrade, this);
        this.img_chosen.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onRear, this);
        this.img_not_chosen.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onRear, this);
    }
    private petdata:GoodInfo;
    private pet:Pet;

    public img_world_bg:eui.Image;
    public img_pet:eui.Image;
    public img_adopt:eui.Image;
    public group_adopt:eui.Group;
    public img_adopt_money:eui.Image;
    public group_adopt_cost:eui.BitmapLabel;

    public group_upgrade:eui.Group;
    public img_upgrade:eui.Image;
    public img_upgrade_money:eui.Image;
    public group_upgrade_money:eui.BitmapLabel;

    public img_page_right:eui.Image;
    public img_page_left:eui.Image;
    public img_list_bg:eui.Image;
    public mission_list:eui.List;

    public lab_name:eui.Label;
    public group_lv:eui.BitmapLabel;
    public img_inrear:eui.Image;
    public img_not_rear:eui.List;
    public img_not_chosen:eui.Image;
    public daily_mission:eui.Image;
    public img_adopt_mission:eui.Image;
    public img_chosen:eui.List;

    public group_next:eui.Group;
    public gro_hair_speed:eui.BitmapLabel;
    public pet_word:eui.Image;
    public img_remind_txt:eui.Image;
    public img_male:eui.Image;
    public img_female:eui.Image;
    public img_remind:eui.Image;
    public level_max:eui.Image;
public img_guild_txt:eui.Label;
    public partAdded(name:string,instance:any){
        super.partAdded(name, instance);
        if( name == "img_world_bg"){
            this.img_world_bg = instance;
            AnchorUtil.setAnchor(this.img_world_bg, 0.5,0.5);
        }else if( name == "img_guild_txt"){
            this.img_guild_txt = instance;
        }else if( name == "level_max"){
            this.level_max = instance;
        }else if( name == "img_pet"){
            this.img_pet = instance;
        }else if( name == "group_adopt"){
            this.group_adopt = instance;
        }else if( name == "img_adopt_money"){
            this.img_adopt_money = instance;
        }else if( name == "group_adopt_cost"){
            this.group_adopt_cost = instance;
        }else if( name == "img_adopt"){
            this.img_adopt = instance;
        }else if( name == "group_upgrade"){
            this.group_upgrade = instance;
        }else if( name == "img_upgrade"){
            this.img_upgrade = instance;
        }else if( name == "img_upgrade_money"){
            this.img_upgrade_money = instance;
        }else if( name == "group_upgrade_money"){
            this.group_upgrade_money = instance;
        }else if( name == "img_page_right"){
            this.img_page_right = instance;
        }else if( name == "img_page_left"){
            this.img_page_left = instance;
        }else if( name == "img_list_bg"){
            this.img_list_bg = instance;
            this.img_list_bg.scale9Grid = new egret.Rectangle(20,16,1,1);
            this.img_list_bg.width = 460;
            this.img_list_bg.height = 238;
            this.img_list_bg.cacheAsBitmap = true;
        }else if( name == "mission_list"){
            this.mission_list = instance;
            this.mission_list.itemRenderer = DailyMissionItemRender;
            this.mission_list.dataProvider = this.missionProvider;

        }else if( name == "lab_name"){
            this.lab_name = instance;
        }else if( name == "group_lv"){
            this.group_lv = instance;
        }else if( name == "img_inrear"){
            this.img_inrear = instance;
        }else if( name == "img_not_rear"){
            this.img_not_rear = instance;
        }else if( name == "img_not_chosen"){
            this.img_not_chosen = instance;
        }else if( name == "img_chosen"){
            this.img_chosen = instance;
        }else if( name == "daily_mission"){
            this.daily_mission = instance;
        }else if( name == "img_adopt_mission"){
            this.img_adopt_mission = instance;
        }else if( name == "group_next"){
            this.group_next = instance;
        }else if( name == "gro_hair_speed"){
            this.gro_hair_speed = instance;
        }else if( name == "pet_word"){
            this.pet_word = instance;
        }else if( name == "img_remind_txt"){
            this.img_remind_txt = instance;
        }else if( name == "img_male"){
            this.img_male = instance;
        }else if( name == "img_female"){
            this.img_female = instance;
        }else if( name == "img_remind"){
            this.img_remind = instance;
        }
    }
    private curPage:number=0;
    private onPage(evt:egret.TouchEvent){
        if( evt.target == this.img_page_left){
            this.curPage--;
            this.rotateTo(1);
        }else{
            this.curPage++;
            this.rotateTo(0);
            Model.checkGuide("page_right");
        }
        this.updatePageDisplay();
    }
    private rotateTo(type:number){
        var tw:egret.Tween = egret.Tween.get(this.img_world_bg);
        if( type == 1){
            tw.to({rotation:this.img_world_bg.rotation+72},100);
        }else{
            tw.to({rotation:this.img_world_bg.rotation-72},100);
        }
    }
    private list:GoodInfo[];
    // private dailyMissionItemRender:eui.ClassFactory;
    private missionProvider:eui.ArrayCollection;
    public updatePageDisplay(){
        if( !this.list){
            if(Model.me.myType == Model.GOOD_TYPE_CAT){
                this.list = Model.cats.concat(Model.dogs);
            }else{
                this.list = Model.dogs.concat(Model.cats);
            }
        }
        var num;
        this.img_page_left.visible = this.curPage > 0;
        this.img_page_right.visible = this.curPage < this.list.length-1;
        this.petdata = this.list[this.curPage];
        this.pet = Model.getPetById(this.petdata.id);
        var url:string;
        this.updateRearInfo();
        if( this.pet != null){
            this.img_male.visible = this.pet.sex == 0;
            this.img_female.visible = this.pet.sex == 1;
            this.lab_name.text = this.pet.name;
            this.group_upgrade.visible = true;
            this.group_adopt.visible = false;
            this.group_lv.text = this.pet.grade+"";
            url = GameConst.Res_Root+"resource/assets/petpreview/"+this.petdata.id+".png";
            num = this.pet.gradeHair;
            if( this.pet.grade < 100){
                this.img_upgrade_money.visible = true;
                this.img_upgrade_money.source = this.petdata.type == Model.GOOD_TYPE_CAT ? "img_money_cat" : "img_money_dog";
                this.img_upgrade.visible = true;
                this.pet_word.visible = false;
                this.group_next.visible = true;
                this.gro_hair_speed.text = this.pet.nextAutoHairPer+"s";
                this.group_upgrade_money.text = GetNumString(num,false,true);
            }else{
                this.img_upgrade_money.visible = false;
                this.pet_word.visible = true;
                this.group_next.visible = false;
                this.pet_word.source = "max_level_remind";
                this.img_upgrade.visible = false;
            }
        }else {
            this.img_male.visible = this.img_female.visible = false;
            this.pet_word.visible = true;
            this.group_next.visible = false;
            this.pet_word.source = "w_"+this.petdata.id+"";
            url = GameConst.Res_Root+"resource/assets/shadow/"+this.petdata.id+".png";
            if(this.petdata.classify == 12){
                this.group_adopt_cost.visible = this.img_adopt_money.visible = false;
            }else{
                this.group_adopt.visible = true;
                this.group_upgrade.visible = false;
                this.group_adopt_cost.visible = this.img_adopt_money.visible = true;
                this.lab_name.text = "???";
                this.group_lv.text = "0";
                if( this.petdata.byDiamond){
                    this.img_adopt_money.source = "img_diamond";
                }else{
                    this.img_adopt_money.source = this.petdata.type == Model.GOOD_TYPE_CAT ? "img_money_cat" : "img_money_dog";
                }
                if( this.petdata.type == Model.me.myType){
                    if( this.petdata.byDiamond ){
                        num = this.petdata.price;
                    }else{
                        num = this.petdata.hair;
                    }
                    this.group_adopt_cost.text = GetNumString(num,false,true);
                }else{
                    if( this.petdata.byDiamond ){
                        num =  this.petdata.price;
                    }else{
                        num = this.petdata.hair;
                    }
                    this.group_adopt_cost.text = GetNumString(num,false,true);
                }
            }
        }

        if( url != this.img_pet.source)
            this.img_pet.source = url;

        this.img_remind.visible = this.daily_mission.visible = this.pet != null;
        this.img_adopt_mission.visible = this.pet == null;
        if( Model.UnlockConfigs )
            this.updateList();
    }
    public updateRearInfo(){
        var petdata:GoodInfo = this.list[this.curPage];
        var pet:Pet = Model.getPetById(petdata.id);
        this.img_not_chosen.visible = pet != null && !pet.rear;
        this.img_not_rear.visible = !pet || !pet.rear;
        this.img_chosen.visible = this.img_inrear.visible = pet && pet.rear;
    }
    public updateList(){
        if( this.pet ){//adopted
            this.mission_list.visible = true;
            this.missionProvider.source = Model.curMissions;
            this.img_remind_txt.visible = false;
        }else{
            var fuck = Model.GetUnlockConfig(this.petdata.id);
            var missions:number[] = this.getMissions(fuck);
            if( this.petdata.classify == 12 ){
                this.mission_list.visible = true;
                this.missionProvider.source = missions;
                this.img_remind_txt.visible = false;
            }else if( this.curPage <= this.getGap()){
                this.mission_list.visible = true;
                this.missionProvider.source = missions;
                this.img_remind_txt.visible = false;
            }else{
                this.mission_list.visible = false;
                this.img_remind_txt.visible = true;
            }
        }
    }
    public getGap():number{
        var len = Model.pets.length;
        var referLen ;
        if( Model.me.myType == Model.GOOD_TYPE_DOG){
            referLen = len - Model.GuildDogCnt;
            if( referLen < 5){
                return referLen;
            }else{
                return  referLen + Model.TotalGuildDogCnt;
            }
        }else{
            referLen = len - Model.GuildCatCnt;
            if( referLen < 5){
                return referLen;
            }else{
                return  len + Model.TotalGuildCatCnt;
            }
        }
    }
    private onAdopt(evt:egret.Event){
        SoundMgr.getInstance().playEffect(SoundMgr.BTN);
        var fuck = Model.GetUnlockConfig(this.petdata.id);
        var missions:number[] = this.getMissions(fuck);
        var pro = this.getProgress(missions);
        var adoptable:boolean = this.checkAdoptable(this.petdata);
        if( pro < missions.length){
            ShowHint("主银，领养宠物需要先完成对应的领养任务噢！");
        }else if( !adoptable ){
            if( this.petdata.byDiamond)
                ShowHint("只要"+this.petdata.price+"钻石就能领我回家");
            else
                ShowHint("还是多产点毛再来领养我吧");
        }else{
            BuyPetPanel.getInstance().showPet(this.petdata);
        }
    }
    private checkAdoptable(petdata:GoodInfo):boolean{
        var result:boolean;
        if( petdata.byDiamond ){
            result = Model.me.diamond > petdata.price;
        }else{
            result = (this.petdata.type == Model.GOOD_TYPE_CAT ? Model.me.catHair : Model.me.dogHair) > petdata.hair;
        }
        return result;
    }

    private onUpgrade(evt:egret.Event){
        SoundMgr.getInstance().playEffect(SoundMgr.BTN);
        var petdata:GoodInfo = this.list[this.curPage];
        var pet:Pet = Model.getPetById(petdata.id);
        var total:number = pet.type == Model.GOOD_TYPE_CAT ? Model.me.catHair : Model.me.dogHair;
        //if( Model.GuideType == "upgrade" && this.sended) return;
        if(pet.gradeHair <= total){
            //this.sended = true;
            window["__upgrade"](petdata.id);
        }else{
            ShowHint("升级所需资源不足");
        }
    }

    public onRear(evt:egret.TouchEvent){
        SoundMgr.getInstance().playEffect(SoundMgr.BTN);
        var petdata:GoodInfo = this.list[this.curPage];
        var pet:Pet = Model.getPetById(petdata.id);
        if( PetMgr.getInstance().pets.length == 1 && pet.rear){
            ShowHint("主银，您不能赶我走哇  /(ㄒoㄒ)/~~");
            return;
        }
        if(pet != null){
            window["__changeRear"](pet.petId);
        }
    }
    private getProgress(missions:number[]):number{
        var len = missions.length;
        var result = 0;
        var mission:MissionInfo;
        for(var i =0; i< len; i++){
            mission  = Model.unlockModels[missions[i]];
            if( Model.finishedMissions.indexOf(missions[i])!= -1 || mission.status == 2)
                result++;
        }
        return result;
    }
    private getMissions(config:any):number[]{
        var list = config.config.configs;
        var len = list.length;
        var info;
        for(var i = 0; i < len; i++ ){
            info = list[i];
            if( info.type == 7 ){
                return info.missionIds;
            }
        }
        return [];
    }

    private static _instance:TravelPanel;
    public static getInstance():TravelPanel{
        if( TravelPanel._instance == null )
            TravelPanel._instance = new TravelPanel();
        return TravelPanel._instance;
    }
}